package com.sumapay.fsb.gensrc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.jdbc.BlobFromLocator;
import com.sumapay.fsb.gensrc.entity.FsbUsers;
import com.sumapay.fsb.gensrc.mapper.FsbUsersMapper;
import com.sumapay.fsb.gensrc.service.IFsbSmsRequestService;
import com.sumapay.fsb.gensrc.service.IFsbUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import javax.sql.rowset.serial.SerialBlob;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suma
 * @since 2021-03-05
 */
@Service
public class FsbUsersServiceImpl extends ServiceImpl<FsbUsersMapper, FsbUsers> implements IFsbUsersService {
    /**
     *判断用户是否存在
     * **/
    @Override
    public boolean isExist(String phone_no) {
        QueryWrapper<FsbUsers> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PHONE_NO", phone_no);
        if(baseMapper.selectOne(queryWrapper)!=null)
            return true;
        return false;
    }


    /**
     * 注册
     * */
    @Override
    public String register(MultiValueMap<String, Object> map) {
        Map<String,Object> returnMap = new HashMap<>();

        //检查是否重复注册
        returnMap.put("ACTION_RESULT",1);
        String phoneNo = map.get("PHONE_NO").get(0).toString();
        if(isExist(phoneNo)){
            returnMap.put("ERROR_MESSAGE","该用户已存在");
            return JSON.toJSONString(map);
        }

        //检查验证码是否输入正确
        if(!iFsbSmsRequestService.checkSms(map)){
            returnMap.put("ERROR_MESSAGE","验证码输入错误或已超时,请检查短信验证码");
            return JSON.toJSONString(map);
        }

        FsbUsers user = new FsbUsers();
        user.setPhoneNo(phoneNo);
        user.setLoginPassword(map.get("LOGIN_PASSWORD").get(0).toString());
        user.setStatus(0);
        if(baseMapper.insert(user)>0){
            returnMap.put("ACTION_RESULT",0);
            returnMap.put("UID",user.getId());
        }else{
            returnMap.put("ERROR_MESSAGE","服务器错误,请联系客服或稍后重试!");
        }
        return JSON.toJSONString(returnMap);
    }


    /**
     * 登陆
     * */
    @Override
    public String login(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ACTION_RESULT",1);

        QueryWrapper<FsbUsers> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PHONE_NO", map.get("PHONE_NO").toString());
        queryWrapper.eq("LOGIN_PASSWORD", map.get("LOGIN_PASSWORD").toString());
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        FsbUsers fsbUsers = baseMapper.selectOne(queryWrapper);
        if(fsbUsers!=null){
            returnMap.put("STATUS",fsbUsers.getStatus());
            returnMap.put("ACTION_RESULT",0);
        }else {
            returnMap.put("ERROR_MESSAGE","用户名或密码错误");
        }
        return JSON.toJSONString(returnMap);

    }
    /**
     * 实名认证查询
     * **/

    @Override
    public String isVerified(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ACTION_RESULT",1);

        QueryWrapper<FsbUsers> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PHONE_NO", map.get("PHONE_NO").toString());
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        FsbUsers fsbUsers = baseMapper.selectOne(queryWrapper);
        if(fsbUsers!=null){
            returnMap.put("STATUS",fsbUsers.getStatus());
            returnMap.put("REAL_NAME",fsbUsers.getRealName());
            returnMap.put("ID_NO",fsbUsers.getIdNumber());
            returnMap.put("ACTION_RESULT",0);
        }else {
            returnMap.put("MESSAGE","查询错误，请稍后重试！");
        }
        return JSON.toJSONString(returnMap);

    }

    /**
     *实名认证
     * **/

    @Override
    public String authenticate(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("ACTION_RESULT",1);
        QueryWrapper<FsbUsers> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PHONE_NO", map.get("PHONE_NO").toString());
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        FsbUsers fsbUsers = baseMapper.selectOne(queryWrapper);
        queryWrapper.clear();
        fsbUsers.setIdPicFront((byte[]) map.get("ID_PIC_FRONT"));
        fsbUsers.setIdPicReverse((byte[]) map.get("ID_PIC_REVERSE"));
        fsbUsers.setRealName(map.get("REAL_NAME").toString());
        fsbUsers.setIdNumber(map.get("ID_NUMBER").toString());
        fsbUsers.setStatus(1);
        if(baseMapper.updateById(fsbUsers)>0){
            returnMap.put("ACTION_RESULT",0);
            returnMap.put("STATUS",3);
        }else{
            returnMap.put("MESSAGE","服务器异常，请稍后重试");
        }

        return JSON.toJSONString(returnMap);

    }

    private IFsbSmsRequestService iFsbSmsRequestService;

    @Autowired
    public void setiFsbSmsRequestService(IFsbSmsRequestService iFsbSmsRequestService){
        this.iFsbSmsRequestService = iFsbSmsRequestService;
    }

}
